
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "wlsdk_define.h"
#include "wlsdk.h"
#include "linux/input.h"
#include "fcntl.h"

#define LOG(format, ...) printf(format"\n", ##__VA_ARGS__)

// 目前这是测试服的数据
#define WEILA_LOGIN_URL         "http://demo.weila.hk/v1/msg-server"
#define WEILA_WEB_SERVER_URL    "demo.weila.hk"
#define WEILA_WEB_SERVER_PORT   80
#define WEILA_APP_ID            "102036"
#define WEILA_APP_KEY           "b3c658bd2e637c65efb134fb381d4a18"
#define WEILA_PRODUCT_NAME      "CF02"      // 微喇服务器认可名称


int isLogin = 0;

void wlsdk_msg_callback(wl_uint32 msgid, wl_int32 resultcode, void *msgdata, wl_uint32 datalen) {
    LOG("msgid = %d, resultcode = %d, datalen = %d", msgid, resultcode, datalen);

    switch(msgid) {
        case WLSDK_MSG_LOGIN_BIND_EXTENSION: {
            wlsdk_extension_bind *pInfo = (wlsdk_extension_bind*)msgdata;
            LOG("pInfo->extensionNo = %s", pInfo->nick);
            // APP绑定请求信息
            wlsdk_answer_bind_extension(1);
        }
        break;

        case WLSDK_MSG_LOGIN_ANSWER_BIND_EXTENSION: {
            // 绑定结果应答
            LOG("WLSDK_MSG_LOGIN_ANSWER_BIND_EXTENSION resultcode = %d", resultcode);
        }
        break;

        case WLSDK_MSG_LOGIN_EXTENSION_STATE: {
            // 登录状态消息
            wlsdk_extension_state *pState = (wlsdk_extension_state*)msgdata;
            LOG("pState = %d", *pState);
            if (*pState == WLSDK_EXTENSION_LOCK) {
                // 设备被锁定
            }else if (*pState == WLSDK_EXTENSION_NOBIND) {
                // 设备未绑定
            }else if (*pState == WLSDK_EXTENSION_BIND) {
                // 设备已绑定
            }
        }
        break;

        case WLSDK_MSG_LOGIN_EXTENSION_VERCODE: {
            char* pVericode = (char*)malloc(datalen + 1);
            memset(pVericode, 0, datalen + 1);
            memcpy(pVericode, msgdata, datalen);
            LOG("pVericode = %s", pVericode);
            // 设备收到的验证码
        }
        break;

        case WLSDK_MSG_LOGIN_STATE: {
            wlsdk_login_state* pState = (wlsdk_login_state*)msgdata;
            int state = *pState;
            LOG("WLSDK_MSG_LOGIN_STATE state = %d", state);
            switch (*pState) {
                case WLSDK_LOGIN_LOGINING: {
                    // 登录中
                    LOG("WLSDK_LOGIN_LOGINING");
                }
                break;
                case WLSDK_LOGIN_ONLINE: {
                    // 登录成功
                    LOG("WLSDK_LOGIN_ONLINE");
                    isLogin = 1;
                }
                break;
                case WLSDK_LOGIN_LOGOUT: {
                    // 登出
                    LOG("WLSDK_LOGIN_LOGOUT");
                }
                break;
                case WLSDK_LOGIN_KICKOUT: {
                    // 被踢
                    LOG("WLSDK_LOGIN_KICKOUT");
                }
                break;
                case WLSDK_LOGIN_EXCEPTION: {
                    // 登录异常
                    LOG("WLSDK_LOGIN_EXCEPTION");
                }
                break;
            }
        }
        break;

        case WLSDK_MSG_SESSION_GET_SESSIONS: {
            wlsdk_session_list* pList = (wlsdk_session_list*)msgdata;

            LOG("WLSDK_MSG_SESSION_GET_SESSIONS resultcode = %d datalen = %d", resultcode, datalen);
            if (resultcode == 0 && datalen) {
                LOG("pList->active_sessionid = %lld", pList->active_sessionid);
                LOG("pList->active_sessiontype = %d", pList->active_sessiontype);
                // 获取会话列表

                LOG("pList->session_count = %d", pList->count);
                // for (int i = 0; i < pList->count; i++) {
                //     wlsdk_session* pSession = &pList->sessions[i];
                //     LOG("pSession->name = %s", pSession->name);
                //     LOG("pSession->sessionid = %d", pSession->sessionid);
                //     LOG("pSession->sessiontype = %d", pSession->sessiontype);
                //     wlsdk_set_burst_session(pSession->sessionid, pSession->sessiontype);
                // }
            }
        }
        break;

        case WLSDK_MSG_SESSION_BURST_STATE: {
            // 通话状态
            wlsdk_session_burst *pBurstState = (wlsdk_session_burst*)msgdata;
            LOG("pBurstState->burststate = %d", pBurstState->burststate);
        }
        break;

        case WLSDK_MSG_SESSION_BURST_SESSION: {
            // 当前会话信息
            wlsdk_session* pSession = (wlsdk_session*)msgdata;
            LOG("pSession->name = %s", pSession->name);
            LOG("pSession->sessionid = %lld", pSession->sessionid);
            LOG("pSession->sessiontype = %d", pSession->sessiontype);
        }
        break;

        case WLSDK_MSG_LOCATION_REQUEST_REALTIME:
        case WLSDK_MSG_LOCATION_REQUEST_TRACK:
        case WLSDK_MSG_LOCATION_REQUEST_SOS: {
            //TODO: 上报小区信号信息或GPS经纬信息
        }
        break;

        default:
            break;
    }
}

void wlsdk_trace_callback(wl_char* buf) {
    LOG("%s", buf);
}


void msleep(int ms) {
    usleep(ms * 1000);
}


int main()
{
    int count = 10;
    int ret = 0;

    ret = wlsdk_init(wlsdk_msg_callback, wlsdk_trace_callback);
    LOG("wlsdk_init ok: %d ?", ret);


    ret = wlsdk_set_appkey(WEILA_APP_ID, WEILA_APP_KEY);
    LOG("wlsdk_set_appkey ok: %d ?", ret);

    // 请使用正确的IMEI和IMSI
    ret = wlsdk_set_hardware("1", WEILA_PRODUCT_NAME, "0010680000001234", "123456789012345", "PTW_V1.0", 1, "PTW Core", 6);
    LOG("wlsdk_set_hardware ok: %d ?", ret);

    ret = wlsdk_set_login_url(WEILA_LOGIN_URL);
    LOG("wlsdk_set_login_url ok: %d ?", ret);

    ret = wlsdk_set_web_server(WEILA_WEB_SERVER_URL, WEILA_WEB_SERVER_PORT);
    LOG("wlsdk_set_web_server ok: %d ?", ret);

    ret = wlsdk_set_network(0, 0, WLSDK_SYSTEM_NETWORK_CONNECTED);
    LOG("wlsdk_set_network ok: %d ?", ret);
    

    int keyEvent = open("/dev/input/event17", O_RDONLY);
    if (keyEvent < 0) {
        perror("open /dev/input/event12 fail");
        return -1;
    }

    struct input_event event;
    while (1) {
        if (read(keyEvent, &event, sizeof(event)) < 0) {
            perror("read /dev/input/event17 fail");
            return -1;
        }

        if (event.type == EV_KEY) {
            if (isLogin) {
                if (event.code == KEY_LEFTSHIFT) {
                    if (event.value == 1) {
                        printf("KEY_SPACE press\n");
                        wlsdk_session_burst_request();
                    }else if (event.value == 0) {
                        printf("KEY_SPACE release\n");
                        wlsdk_session_burst_release();
                    }
                }
            }
        }
    }
    
    return 0;
}
